<template>
    <Modal
        :value="mstatus"
        title="开启竞拍"
        width="600"
        :loading="loading"
        @on-ok="ok('formValidate')"
        @on-cancel="cancel">
        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
            <FormItem label="联系人" prop="contacts">
                <Input v-model="formValidate.contacts" placeholder="请输入姓名"></Input>
            </FormItem>
            <FormItem label="联系电话" prop="tel">
                <Input v-model="formValidate.tel" placeholder="请输入联系电话"></Input>
            </FormItem>
            <FormItem label="竞拍底价" prop="min_price">
                <Input v-model="formValidate.min_price" placeholder="请输入竞拍底价"></Input>
            </FormItem>
            <FormItem label="选择场次" prop="room_id">
                <Select @on-change="getRoomTime" v-model="roomName" placeholder="选择场次" style="width: 150px">
                    <Option :value="item" v-for="(item,index) in roomListData" :key="index">{{item}}</Option>
                </Select>
                <Select :disabled="!roomName" v-model="formValidate.room_id" placeholder="选择场次时段">
                    <Option :value="item.id" v-for="item in roomTimeListData" :key="item.id">
                        开始时间:{{item.stime | initTime}}~{{item.etime | initTime}};
                        报名时间:{{item.enroll_stime | initTime}}~{{item.enroll_etime | initTime}}
                    </Option>
                </Select>
            </FormItem>
        </Form>
    </Modal>
</template>
<script>
    import API from '../../config/api'

    export default {
        props: ['auctionStatus', 'carData'],
        data() {
            return {
                loading: false,
                roomListData: [],
                roomName: '',
                roomTimeListData: [],
                formValidate: {},
                localData: {},
                ruleValidate: {
                    contacts: [
                        {required: true, message: '联系人', trigger: 'blur'}
                    ],
                    tel: [
                        {required: true, message: '联系电话', trigger: 'blur'},
                    ],
                    room_id: [
                        {required: true, message: '请选择场次', trigger: 'change'}
                    ],
                    min_price: [
                        {required: true, message: '竞拍底价', trigger: 'blur'},
                    ]
                }
            }
        },
        methods: {
            ok(name) {
                this.loading = true;
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.localData = {
                            "car_id": this.carData.goods_id,
                            "car_name": this.carData.goods_allname,
                            "car_no": this.carData.goods_carnumber,
                            "dealer_car_name": this.carData.dealer_contacts,
                            "dealer_id": this.carData.goods_dealid,
                            "dealer_name": this.carData.dealer_name,
                            "role_type": "1",
                            "car_mainimg": this.carData.goods_mainpic,
                        }
                        if(!this.localData.car_no){
                            this.localData.car_no = '';
                        }
                        let data = Object.assign(this.localData, this.formValidate);
                        this.axios.post(API.SAVE_INFO_BY_AUTH_PASS, data).then(res => {
                            this.$emit('closeModal')
                            this.$Message.success(res.data.msg);
                            this.loading = false;
                        }, err => {
                            this.$Message.error(res.data.msg);
                        });
                    } else {
                        this.$Message.error('请完善信息!');
                    }
                })
            },
            cancel() {
                this.$emit('closeModal')
            },
            roomList() {
                this.axios.get(API.GET_ROOM_LIST_USABLE).then(res => {
                    if (res.data.code ===1 ) {
                        this.roomListData = res.data.data;
                    }else {

                    }
                }, err => {
                });
            },
            getRoomTime(name) {
                this.axios.get(API.GET_ROOM_LIST_USABLE_BY_NAME + '?name=' + name).then(res => {
                    this.roomTimeListData = res.data.data;
                }, err => {
                });
            }
        },
        created() {
            this.roomList();
        },
        computed: {
            mstatus() {
                return this.auctionStatus;
            }
        }
    }
</script>
